Michele Volpato

Michele Volpato

Working with drafts in Publish

Tutorials

Publish is a static site generator for Swift developers. You create Markdown files, and then generate the entire website by running a simple sequence of steps like:

try DeliciousRecipes().publish(using: [
    .addMarkdownFiles(),
    .copyResources(),
    .addFavoriteItems(),
    .addDefaultSectionTitles(),
    .generateHTML(withTheme: .delicious),
    .generateRSSFeed(including: [.recipes]),
    .generateSiteMap()
])

If you want to work with draft articles, that is, you want to keep some articles hidden until they are ready, you can easily use branches.

But you can also do it directly in Publish, by creating a custom step.

Prepare your draft article

You need to mark an article as draft, by adding the draft metadata in the front matter:

---
date: 2020-07-13 21:00
...
draft: true
---

and you need to specify it in the WebsiteItemMetadata struct:

struct ItemMetadata: WebsiteItemMetadata {
    var draft: Bool?
}

Define a new step

Defining a new step is easy, in main.swift add:

extension PublishingStep where Site == YourWebsiteName {
    static func removeDrafts() -> Self {
        .step(named: "Remove drafts") { context in
            context.mutateAllSections { section in
                section.removeItems(matching: \.metadata.draft == true)
            }
        }
    }
}

This step will iterate over all section, removing all items that have the draft field, and the field is set to true.

Add the step to the publishing process

Finally you can update the publishing process with the new step.

try DeliciousRecipes().publish(using: [
    .addMarkdownFiles(),
    .removeDrafts(), // <-- we added this.
    .copyResources(),
    .addFavoriteItems(),
    .addDefaultSectionTitles(),
    .generateHTML(withTheme: .delicious),
    .generateRSSFeed(including: [.recipes]),
    .generateSiteMap()
])

In this way you can define more sequences of steps, and choose which one you want to use, based on environment variables, or command line arguments. For instance, you can keep the draft for development, and remove them for deploy in your CI/CD tool.

Get a weekly email about Flutter

Subscribe to get a weekly curated list of articles and videos about Flutter and Dart.

    We respect your privacy. Unsubscribe at any time.

    Leave a comment